package cn.xenosp.server.web.controller;

import cn.xenosp.server.common.Result;
import cn.xenosp.server.common.annotation.LogInfo;
import cn.xenosp.server.common.tools.ObjectConvert;
import cn.xenosp.server.module.dict.entity.SysDictData;
import cn.xenosp.server.module.dict.entity.SysDictDataVO;
import cn.xenosp.server.module.dict.entity.SysDictTypeVO;
import cn.xenosp.server.module.dict.service.SysDictDataService;
import cn.xenosp.server.module.dict.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 项目名： service-web
 * 包路径： cn.xenosp.server.web.controller
 * 作者：   TongHui
 * 创建时间: 2024-11-28 14:33
 * 描述: 字典信息访问层
 * 版本: 1.0
 */
@RestController
@RequestMapping("${app.config.api-base-url}/dict/")
public class DictController {

    @Autowired
    SysDictTypeService sysDictTypeService;

    @Autowired
    SysDictDataService sysDictDataService;

    @LogInfo(operationModuleType = "字典模块",operationName = "分页查询",operationContent = "查询数据字典类型")
    @PostMapping("/getTypePage")
    public Result<?> getTypePage(@RequestBody String body){
        try {
            SysDictTypeVO sysDictTypeVO = ObjectConvert.urlStringToClass(body, SysDictTypeVO.class);
            return Result.OK("",sysDictTypeService.getPage(sysDictTypeVO));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }
    @LogInfo(operationModuleType = "字典模块",operationName = "查询字典类型",operationContent = "查询字典类型")
    @PostMapping("/getDataByDictId")
    public Result<?> getDictDataByType(@RequestBody SysDictTypeVO sysDictTypeVO){
        try {
            List<SysDictData> list = sysDictDataService.getListByDictType(sysDictTypeVO.getType());
            return Result.OK("",list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取字典数据失败",null);
        }
    }

    @LogInfo(operationModuleType = "字典模块",operationName = "更新字典类型",operationContent = "更新字典类型")
    @PostMapping("/saveDictType")
    public Result<?> saveDictType(@RequestBody SysDictTypeVO sysDictTypeVO){
        try {
            sysDictTypeService.update(sysDictTypeVO);
            return Result.OK("保存成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("保存数据失败",null);
        }
    }
    @LogInfo(operationModuleType = "字典模块",operationName = "新增字典类型",operationContent = "新增字典类型")
    @PostMapping("/addDictType")
    public Result<?> addDictType(@RequestBody SysDictTypeVO sysDictTypeVO){
        try {
            sysDictTypeService.save(sysDictTypeVO);
            return Result.OK("添加成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("添加数据失败",null);
        }
    }
    @LogInfo(operationModuleType = "字典模块",operationName = "删除字典类型",operationContent = "删除字典类型")
    @PostMapping("/delDictType")
    public Result<?> delDictType(@RequestBody SysDictTypeVO sysDictTypeVO){
        try {
            sysDictTypeService.delete(sysDictTypeVO);
            return Result.OK("删除成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("删除数据失败",null);
        }
    }

    @LogInfo(operationModuleType = "字典模块",operationName = "更新字典",operationContent = "更新字典数据")
    @PostMapping("/saveDictData")
    public Result<?> saveDictData(@RequestBody SysDictDataVO sysDictDataVO){
        try {
            List<SysDictData> list = sysDictDataService.update(sysDictDataVO);
            return Result.OK("保存成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("保存数据失败",null);
        }
    }
    @LogInfo(operationModuleType = "字典模块",operationName = "新增字典",operationContent = "新增字典数据")
    @PostMapping("/addDictData")
    public Result<?> addDictData(@RequestBody SysDictDataVO sysDictDataVO){
        try {
            List<SysDictData> list = sysDictDataService.save(sysDictDataVO);
            return Result.OK("添加成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("添加数据失败",null);
        }
    }
    @LogInfo(operationModuleType = "字典模块",operationName = "删除字典",operationContent = "删除字典数据")
    @PostMapping("/delDictData")
    public Result<?> delDictData(@RequestBody SysDictDataVO sysDictDataVO){
        try {
            List<SysDictData> list = sysDictDataService.delete(sysDictDataVO);
            return Result.OK("删除成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("删除数据失败",null);
        }
    }
}
